/**
 * Created by Lwang on 2017/3/5.
 * px-rem
 */
import $ from "jquery";
import {way} from "./common.js"
export function square(doc, win) {

    var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
        };
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
}

export function accredit(callback) {
    var _url = ajax.openId, _data = {};
    if (navigator.userAgent.toLowerCase().match(/MicroMessenger/gi) != null) {
        var XMLHttpReq = new XMLHttpRequest();
        XMLHttpReq.open("GET", _url, true);
        XMLHttpReq.withCredentials = true;
        XMLHttpReq.onreadystatechange = function () {
            if (XMLHttpReq.readyState == 4) {
                if (XMLHttpReq.status == 200) {
                    var _json = XMLHttpReq.response;
                    var openId = _json;
                    if (!openId) {
                        let userType = "";
                        if (location.href.match("user-articles") != null) {
                            userType = "normal";
                        } else {
                            userType = "teacher";
                        }
                        location.href = "http://api.studypointshare.com/api/weixin/init?url=" + encodeURIComponent(location.href) + "&userType=" + userType;

                    } else {
                        way._typeOf(callback) == "[object function]" && callback(openId);
                    }
                } else {
                    throw new Error("XMLHttpRequest response filed!!!!!");
                }
            }
        };
        XMLHttpReq.send(null);
    }


}

const prefix = "http://api.studypointshare.com";
export const ajax = {

    //获取教师课程信息
    listCourse: `${prefix}/api/teacher/listCourse`,

    //  登录
    login: `${prefix}/api/teacher/login`,

    //  文件上传
    upload: `${prefix}/api/file/upload`,

    //  添加课程内容
    addContent: `${prefix}/api/teacher/addContent`,

    //  获取课程详情
    course: (id) => `${prefix}/api/teacher/course/${id || 0}`,
    // 浏览者获取课程详情
    userCourse : (id) => `${prefix}/api/user/viewCourse/${id || 0}`,

    //  更改课程
    couseEdit: `${prefix}/api/teacher/couse/edit`,

    //  获取教师信息
    teacherInfo: `${prefix}/api/teacher/info`,

    //  更新教师信息
    modifyTeacher: `${prefix}/api/teacher/modify`,

    //  文件前缀
    file: (id) => `${prefix}/api/file/z/${id}`,

    //  文章详情
    courseDetail: (id) => `${prefix}/api/teacher/course/${id}`,

    //  默认图片
    mainPic: "http://www.studypointshare.com/static/img/main-pic.jpg",
    //  微信config获取
    wxConfig: `${prefix}/api/weixin/config`,
    //  微信上传
    uploadVoice: `${prefix}/api/weixin/voice`,
    //课程删除
    delCourse: (id) => `${prefix}/api/teacher/delCourse/${id}`,
    //    课程内容删除
    contentsDel: `${prefix}/api/teacher/contents/del`,
    //用户注册
    register: `${prefix}/api/teacher/register`,
    //用户登录
    userLogin: `${prefix}/api/teacher/userLogin`,
//    是否重名
    exists: (name) => `${prefix}/api/teacher/exists/${name}`,
    //修改密码
    modifyPsw: `${prefix}/api/teacher/modifyPwd`,
    //微信id接口
    openId: `${prefix}/api/weixin/openId`,
    //授权初始化
    init: `${prefix}/api/weixin/init`
};